package org.darkforest.merchant.controller;

import org.apache.commons.lang3.StringUtils;
import org.darkforest.common.services.SpecialityService;
import org.darkforest.common.util.DateUtil;
import org.darkforest.common.util.Utils;
import org.darkforest.dao.mapper.GoodsCommonMapper;
import org.darkforest.dao.mapper.SpecialityMapper;
import org.darkforest.merchant.service.IGoodsService;
import org.darkforest.model.GoodSearchBean;
import org.darkforest.model.GoodsCommon;
import org.darkforest.model.GoodsCommonSearchBean;
import org.darkforest.model.Speciality;
import org.darkforest.ucenter.auth.AuthPassport;
import org.darkforest.vo.AjaxReturnData;
import org.darkforest.vo.StoreSellerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * Created by Administrator on 2017/4/10.
 */
@Controller
@RequestMapping(value = "speciality")
public class SpecialityController {



    @Autowired
    private SpecialityMapper specialityMapper;

    @Autowired
    private GoodsCommonMapper goodsCommonMapper;


    @Autowired
    private SpecialityService specialityService;


    @RequestMapping(value = "specialityList")
    public ModelAndView specialityList(@RequestParam(required = false,defaultValue = "1") int p,String areaName){
        ModelAndView res = new ModelAndView("speciality/speciality_list");
        Speciality speciality = new Speciality();
        speciality.setStart(p);
        speciality.setSpecialityAreaName(StringUtils.isBlank(areaName) ? null : areaName);
        List<Speciality> specialityList = specialityMapper.selectListByCondition(speciality);
        speciality.setResultList(specialityList);
        res.addObject("pageBean",speciality);
        return res;
    }


    @RequestMapping(value = "toAddSpeciality")
    public ModelAndView  toAddSpeciality(){
        ModelAndView res = new ModelAndView("speciality/speciality_add");
        return res;
    }


    @AuthPassport
    @RequestMapping(value = "doAddSpeciality")
    public @ResponseBody boolean doAddSpeciality(Speciality speciality,HttpServletRequest request){
        StoreSellerVo ssv = ((StoreSellerVo) request.getSession().getAttribute("sellerInfo"));

        speciality.setCreaterId(ssv.getMemberId());
        speciality.setCreateName(ssv.getMemberName());
        speciality.setCreateTime(DateUtil.getCurrentDateTime());
        return specialityService.doAddSpeciality(speciality);
    }


    @RequestMapping(value = "toEditSpeciality")
    public ModelAndView toEditSpeciality(Integer specialId){
        ModelAndView res = new ModelAndView("speciality/speciality_edit");
        List<Speciality> specialities = specialityMapper.selectListWithChildrenByCondition(new Speciality(specialId));
        if(specialities.size()==1)
            res.addObject("speciality",specialities.get(0));
        return res;
    }



    @RequestMapping(value = "doEditSpeciality")
    public @ResponseBody boolean doEditSpeciality(Speciality speciality){
        return specialityService.doEditSpeciality(speciality);
    }

    @RequestMapping(value = "delSpeciality")
    public @ResponseBody
    AjaxReturnData delSpeciality(Integer[] ids){
        AjaxReturnData ajaxReturnData = new AjaxReturnData();
        specialityService.delSpeciality(ids);
        ajaxReturnData.setMessage("删除成功");
        ajaxReturnData.setType("succ");
        ajaxReturnData.setUrl("speciality/specialityList");
        return ajaxReturnData;
    }

    /**
     * 为添加活动选择商品
     * @param request
     * @param goodsName
     * @param start
     * @return
     */
    @RequestMapping(value = "chooseSpecialityGoods")
    public ModelAndView chooseSpecialityGoods(HttpServletRequest request,
                                            @RequestParam(required = false, defaultValue = "") String goodsName,
                                            @RequestParam(required = false, defaultValue = "1") String start
    ) throws UnsupportedEncodingException {
        ModelAndView modelAndView = new ModelAndView("speciality/choose_speciality_goods");
        StoreSellerVo sellerVo= (StoreSellerVo)request.getSession().getAttribute("sellerInfo");

//        GoodSearchBean goodSearchBean = new GoodSearchBean();
//        goodSearchBean.setGoodsName(Utils.encodeGetParam(goodsName));
//        goodSearchBean.setStoreId(sellerVo.getStoreId());
//        goodSearchBean.setStart(Integer.parseInt(start));
//        goodSearchBean.setPageSize(8);
//        goodSearchBean.setGoodsType(0);
//        goodSearchBean.setGoodsState(1);
//        goodSearchBean.setGoodsVerify(1);
//        this.goodsService.searchGoodsByPage(goodSearchBean);

        GoodsCommonSearchBean goodsCommonSearchBean = new GoodsCommonSearchBean();
        goodsCommonSearchBean.setGoodsName(new String(goodsName.getBytes("ISO-8859-1"), "UTF-8"));
        goodsCommonSearchBean.setStoreId(sellerVo.getStoreId());
        goodsCommonSearchBean.setStart(Integer.parseInt(start));
        goodsCommonSearchBean.setPageSize(8);
        goodsCommonSearchBean.setGoodsType(0);
        goodsCommonSearchBean.setGoodsVerify(1);
        goodsCommonSearchBean.setGoodsState(1);
        List<GoodsCommon> goodsCommonList = goodsCommonMapper.listGoodsCommonByCondition(goodsCommonSearchBean);
        goodsCommonSearchBean.setResultList(goodsCommonList);
        modelAndView.addObject("pageBean", goodsCommonSearchBean);
        return modelAndView;
    }




}
